import {IValidator} from "./IValidator";
import {mc_validatorResult} from "../model/mc_validatorResult";
import {requireValidator} from "./requireValidator";
/**
 * Created by ybjaa on 2017/6/28.
 */
export abstract class abstractValidator implements IValidator{

    protected messagePrefix:string="";
    protected result:mc_validatorResult=new mc_validatorResult();


    protected abstract getValidator(value: number | string | Object,thisObj:Object): mc_validatorResult;


    protected setResult(result:boolean=false,message:string=""):void{
        if(this.result){
            this.result.result=result;
            if(!result){
                this.result.message=this.messagePrefix+message;
            } else {
                this.result.message=message;
            }
        }
    }

    public validator(value: number | string | Object,thisObj:Object=null):mc_validatorResult{

        //判断是否为空
        let validator=new requireValidator();
        this.setValidatorClass(validator);
        let result=validator.validator(value);
        if(!result.result) return result;

        this.result=new mc_validatorResult();

        return this.getValidator(value,thisObj);

    }

    public setMessagePrefix(message:string=""){
        message=message||"";
        this.messagePrefix=message.trim();
    }

    public setValidatorClass(validator:abstractValidator):abstractValidator{
        validator.setMessagePrefix(this.messagePrefix);
        return validator;
    }


    public setParameters(...args:Array<string|number|Object>): void {

    }


}